import { IDomEditor, SlateDescendant, SlateElement } from '@wangeditor/editor'
import { ActivityElement } from './custom-types'
import { ActivityElementTypeName } from './config'

// setHTML 解析到 富文本

function parseHtml(
    elem: Element,
    children: SlateDescendant[],
    editor: IDomEditor
): SlateElement {
    const href = elem.getAttribute('href') || ''
    const id = elem.getAttribute('actId') || ''
    const miniprogramAppid = elem.getAttribute('data-miniprogram-appid') || ''
    const miniprogramPath = elem.getAttribute('data-miniprogram-path') || ''
    const name = elem.textContent

    const activityElement = {
        type: ActivityElementTypeName,
        href,
        id,
        miniprogramAppid,
        miniprogramPath,
        name,
        children: [{ text: '' }], // void node 必须有一个空白 text
    } as ActivityElement

    return activityElement
}

const parseHtmlConf = {
    selector: `a[data-w-e-type="${ActivityElementTypeName}"]`,
    parseElemHtml: parseHtml,
}

export default parseHtmlConf
